package intelligentCustomer.planning;

import intelligentCustomer.reasoning.ReasoningEngine;
import intelligentCustomer.reasoning.ReasoningResult;

/**
 * 基于推理的计划步骤
 * 将推理引擎集成到计划步骤中
 */
public class ReasoningBasedPlanStep implements PlanStep {
    private final String name;
    private final ReasoningEngine reasoningEngine;
    private final String reasoningType;
    private int reasoningDepth = 2; // 推理深度，可调整
    
    public ReasoningBasedPlanStep(String name, ReasoningEngine reasoningEngine, String reasoningType) {
        this.name = name;
        this.reasoningEngine = reasoningEngine;
        this.reasoningType = reasoningType;
    }
    
    @Override
    public String execute(PlanningContext context) {
        // 构建推理上下文
        String query = context.getQuery();
        String intent = context.getIntent().getCategory();
        
        // 收集前序步骤的结果作为推理输入
        StringBuilder reasoningInput = new StringBuilder();
        reasoningInput.append("查询: ").append(query).append("\n");
        reasoningInput.append("意图: ").append(intent).append("\n");
        
        // 添加前序步骤的结果
        for (String key : context.getResults().keySet()) {
            reasoningInput.append(key).append(": ").append(context.getResult(key)).append("\n");
        }
        
        // 执行推理
        ReasoningResult result = reasoningEngine.reason(
            reasoningInput.toString(),
            reasoningType,
            reasoningDepth
        );
        
        // 保存推理过程到上下文
        context.setAttribute("reasoningProcess:" + name, result.getReasoningProcess());
        
        return result.getConclusion();
    }
    
    @Override
    public String getName() {
        return name;
    }
    
    @Override
    public void adjustParameters(PlanningContext context) {
        // 根据重试次数增加推理深度
        reasoningDepth = Math.min(5, reasoningDepth + 1);
    }
} 